/**
 * Created by xuxujunjunforever on 2015/12/9.
 */
'use strict'

let config = require('../../../config');
let errors = config.errors;

class Response {
    json(obj) {
        this.result = obj;
    }
}
let session = {};

function login(req, res) {
    let user = req.body.user;
    if ( user.name === 'test' && user.password === 'e10adc3949ba59abbe56e057f20f883e' ) {
        res.json({
            successful: true
        });
    }
    else {
        throw errors.UsernamePasswordMismatched;
    }
}

function actionLoginSuccessful(test) {
    let req = {
        "body":{
            "user": {
                "name": "test",
                "password": "e10adc3949ba59abbe56e057f20f883e"
            }
        }
    };

    let res = new Response();

    login(req, res);

    let result = res.result;
    test.ok(result.successful);

    test.done();
}

function actionLoginNamePasswordMismatched(test) {
    let req = {
        "body":{
            "user": {
                "name": "test",
                "password": "0"
            }
        }
    };

    let res = new Response();

    test.throws(function() {
        login(req, res);
    }, errors.classes.UsernamePasswordMismatched);

    test.done();
}

function throwWrongExceptionSample(test) {
    test.throws(function() {
        throw errors.InternalException;
    }, errors.classes.UsernamePasswordMismatched);

    test.done();
}

module.exports = {
    actionLoginSuccessful,
    actionLoginNamePasswordMismatched,
    throwWrongExceptionSample
};
